package org.beehoo.web.daily.signin.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.beehoo.web.daily.signin.dao.SignInDAO;
import org.beehoo.web.daily.usermanage.po.UserPO;
import org.beehoo.web.daily.usermanage.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class SignInService {
	@Autowired
	private SignInDAO signInDAO;
	
	public boolean validateSameUserName(String userName){
		UserPO po = signInDAO.getUser(userName);
		if(po == null){
			return false;
		}
		return true;
	}
	
	/**
	 * 
	 * 保存用户信息
	 *
	 * @param user 用户DTO
	 * @author Tianhl
	 * @created 2011-8-1 下午03:03:12
	 */
	public UserVO signIn(UserVO user){
		UserPO po = new UserPO();
		BeanUtils.copyProperties(user, po);
		po.setOperateTime(new Date());
		po.setOperator(po.getUserName());
		signInDAO.saveOrUpdate(po);
		user.setId(po.getId());
		return user;
	}
	
	public UserVO getUser(String userName){
		UserPO po = signInDAO.getUser(userName);
		UserVO vo = null;
		if(po != null){
			vo = new UserVO();
			BeanUtils.copyProperties(po, vo);
		}
		return vo;
	}
	
	public List<UserVO> getUserList(){
		List<UserPO> poList = signInDAO.getUserList();
		List<UserVO> voList = new ArrayList<UserVO>();
		for(UserPO po:poList){
			UserVO vo = new UserVO();
			BeanUtils.copyProperties(po, vo);
			voList.add(vo);
		}
		return voList;
	}
}
